package com.srs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.Group;
import com.srs.model.RGroupScope;
import com.srs.util.HibernateUtil;

public class RGroupScopeDao {

	final static Logger logger = LoggerFactory.getLogger(RGroupScopeDao.class);

	private List<RGroupScope> rGroupScopeList;
	private RGroupScope rGroupScope;

	public void delete(Integer rGroupScopeId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			rGroupScope = (RGroupScope) session.get(RGroupScope.class, rGroupScopeId);
			session.delete(rGroupScope);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	public void deleteByGroupId(Integer groupId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query q = session.createQuery("delete RGroupScope r where r.group.groupId= :groupId");
			q.setInteger("groupId", groupId);
			q.executeUpdate();			
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	@SuppressWarnings("unchecked")
	public List<RGroupScope> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			rGroupScopeList = session.createQuery("from RGroupScope").list();
			return rGroupScopeList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<RGroupScope> list(Group group) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from RGroupScope r where r.group.groupId=:groupId");
			q.setInteger("groupId", group.getGroupId());
			rGroupScopeList = q.list();
			return rGroupScopeList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}	


	@SuppressWarnings("unchecked")
	public boolean scopeIsUsed(Integer scopeId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from RGroupScope r where r.scopeId=:scopeId");
			q.setInteger("scopeId", scopeId);
			rGroupScopeList = q.list();
			if (rGroupScopeList.size()==0){
				return false;
			}
			return true;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return false;
		} finally {
			session.close();
		}

	}			
	
	public RGroupScope get(Integer rGroupScopeId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from RGroupScope r where r.rGroupScopeId=:rGroupScopeId");
			q.setInteger("rGroupScopeId", rGroupScopeId);
			return (RGroupScope) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(RGroupScope rGroupScope) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(rGroupScope);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(RGroupScope rGroupScope) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(rGroupScope);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
